<?php


namespace app\middleware;
use think\facade\Db;

class Logininfo
{
    public function handle($request, \Closure $next)
    {
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        //设置 redis 字符串数据
        $res = $redis->get(app('getsystem')->MacAddInfo());
        if (!$res) {
            session('member',NULL);
        }

        if ($res) {
            $res = unserialize($res);

            // 生成新的用户
            $res['userid'] = Db::connect('zhu')->table('cms_member')->where('mobile',$res['phone'])->value('userid');

            event('Userup',[$res]);

            $member = Db::table('ea_member')
                ->where('phone',$res['phone'])
                ->field('id,default_identity,name,avatar,sex,phone')
                ->find();

            $member['name'] = $member['name'] ? $member['name'] : '牛人';

            session('member',$member);

            // 判断是否已认证
            $ea_company = Db::table('ea_company_management m')
                    ->join('ea_company c','m.company_id = c.id')
                    ->where('member_id',$member['id'])
                    ->where('status',2)
                    ->find();
            if ($ea_company) {
                session('company',1);
            } else {
                session('company',NULL);
            }
        }

        return $next($request);
    }
}
